package com.lagou;

import com.lagou.bean.ComsumerComponet;
import com.lagou.filter.DubboInvokeFilter;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Arrays;

public class AnnotationConsumerMain {
    public static void main(String[] args) throws Exception {
        System.out.println("-------------");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ComsumerComponet service = context.getBean(ComsumerComponet.class);
//        while (true) {
//            //输入几个字符，就执行几次循环
//            System.in.read();
//            String helloMsg = service.methodA();
//            System.out.println("result:" + helloMsg);
//        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String s = service.methodA();
//                    System.out.println(s);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String s = service.methodB();
//                    System.out.println(s);
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String s = service.methodC();
//                    System.out.println(s);
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //读取三个静态变量的TP90和TP99
//                    Long[] peekA = DubboInvokeFilter.methodADelayQueue.peek();
//                    Long[] peekB = DubboInvokeFilter.methodBDelayQueue.peek();
//                    Long[] peekC = DubboInvokeFilter.methodCDelayQueue.peek();
//                    if (peekA == null || peekB == null || peekC == null) {
//                        continue;
//                    }
//                    System.out.println("methodA最新一次时延是：" + peekA[0]);
//                    System.out.println("methodB最新一次时延是：" + peekB[0]);
//                    System.out.println("methodC最新一次时延是：" + peekC[0]);
//                    System.out.println(DubboInvokeFilter.methodADelayQueue.size());
                    Long methodATP90 = DubboInvokeFilter.getTopPercentileDelay(0.9, DubboInvokeFilter.methodADelayQueue);
                    Long methodATP99 = DubboInvokeFilter.getTopPercentileDelay(0.99, DubboInvokeFilter.methodADelayQueue);
                    Long methodBTP90 = DubboInvokeFilter.getTopPercentileDelay(0.9, DubboInvokeFilter.methodBDelayQueue);
                    Long methodBTP99 = DubboInvokeFilter.getTopPercentileDelay(0.99, DubboInvokeFilter.methodBDelayQueue);
                    Long methodCTP90 = DubboInvokeFilter.getTopPercentileDelay(0.9, DubboInvokeFilter.methodCDelayQueue);
                    Long methodCTP99 = DubboInvokeFilter.getTopPercentileDelay(0.99, DubboInvokeFilter.methodCDelayQueue);

                    System.out.println("methodA最近1分钟的TP90时延是：" + methodATP90 + "毫秒，TP99时延是：" + methodATP99 + "毫秒。");
                    System.out.println("methodB最近1分钟的TP90时延是：" + methodBTP90 + "毫秒，TP99时延是：" + methodBTP99 + "毫秒。");
                    System.out.println("methodC最近1分钟的TP90时延是：" + methodCTP90 + "毫秒，TP99时延是：" + methodCTP99 + "毫秒。");
                }
            }
        }).start();
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
